//
//  MovieListTableViewCell.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "MovieListTableViewCell.h"

@implementation MovieListTableViewCell
- (void)dealloc
{
    [_myImageView release];
    [_myLabel release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView
{
    self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 90, 120)];
    [self addSubview:self.myImageView];
    self.myImageView.backgroundColor = [UIColor yellowColor];
    [_myImageView release];
    
    self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(130, 65, 200, 30)];
    [self addSubview:self.myLabel];
    [_myLabel release];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(330, 15, 24, 24);
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.button];
}

- (void)buttonAction:(UIButton *)btn
{
    if (self.aMovie.selected) {
        [self.button setBackgroundImage:[UIImage imageNamed:@"check@2x.png"] forState:UIControlStateNormal];
        
    }else{
        [self.button setBackgroundImage:[UIImage imageNamed:@"checked@2x.png"] forState:UIControlStateNormal];
    }
    self.aMovie.selected = !self.aMovie.selected;
    //执行协议方法
    [self.movieLVCDelegate movieInfo:self.aMovie];
}

-(void)setAMovie:(Movie *)aMovie
{
    if (_aMovie != aMovie) {
        [_aMovie release];
        _aMovie = [aMovie retain];
    }
    self.myLabel.text = self.aMovie.movieName;
    
    NSURL *url = [NSURL URLWithString:self.aMovie.pic_url];
    [self.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder.png"]];
    
    
    //1.上拉下拉cell会重用cell,所以需要在赋值的时候加判断
    [self.button setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    
    if (_aMovie.selected) {
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    }
    
}








- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
